<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="query">
                <el-form-item>
                    <el-input v-model="query.keywords" placeholder="店铺名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select clearable v-model="query.state" placeholder="店铺状态">
                        <el-option label="待审核" value="0"></el-option>
                        <el-option label="通过" value="1"></el-option>
                        <el-option label="驳回" value="-1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表
            v-loading:开启加载效果

        -->
        <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="宠物名字" width="120" >
            </el-table-column>
            <el-table-column prop="tel" label="电话" width="120" >
            </el-table-column>
            <el-table-column prop="state" label="状态" min-width="80" >
                <template scope="scope">
                    <span v-if="scope.row.state==0" style="color: #8c939d">待审核</span>
                    <span v-if="scope.row.state==1" style="color: #13ce66">审核通过</span>
                    <span v-if="scope.row.state==-1" style="color: #ff4d51">驳回</span>
                </template>
            </el-table-column>
            <el-table-column prop="address" label="地址" >
            </el-table-column>
            <el-table-column label="操作" width="250">
                <template scope="scope">
                    <el-button type="warning" size="small" :disabled="scope.row.state==1" @click="check(scope.$index, scope.row)">审核</el-button>
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                           :page-size="query.pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增、编辑界面
            :rules:定义验证规则


        -->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="部门名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="部门编号" prop="sn">
                    <el-input v-model="addForm.sn" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="1">启用</el-radio>
                        <el-radio class="radio" :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="上级部门">
                    <el-input v-model="addForm.parent_id" auto-complete="off"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
        <!--审核的弹出框-->
        <el-dialog title="审核" :visible.sync="formVisible" :close-on-click-modal="false">
            <el-form :model="dataForm" label-width="80px">
                <el-form-item label="店铺名称" >
                    <el-input v-model="dataForm.name" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="电话" >
                    <el-input v-model="dataForm.tel" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="地址" >
                    <el-input v-model="dataForm.address" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="logo" >
                    <el-input v-model="dataForm.logo" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="驳回原因">
                    <el-input type="textarea" v-model="dataForm.reason"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="formVisible = false">取消</el-button>
                <el-button type="success" @click.native="addSubmit" :loading="addLoading">通过</el-button>
                <el-button type="danger" @click.native="reject" :loading="addLoading">驳回</el-button>
            </div>
        </el-dialog>

    </section>
</template>

<script>
    export default {
        data(){
            return {
                listLoading:false,//开启加载效果
                listData:[],
                sels:[],
                total:0, //数据的总条数
                query:{//分页条件
                    pageSize:5,
                    currentPage:1,
                    keywords:'',
                    state:null

                },
                addFormVisible:false, //默认隐藏弹出框
                addForm:{// 用于数据双向绑定对象
                    name:'',
                    sn:'',
                    state:1,
                    manager:null,//用户记录负责人
                    parent_id:null
                },
                formVisible:false,
                dataForm:{//数据展示
                    reason:"",

                },
                addFormRules: {
                    /*
                        验证规则
                           要求： required: true   必填
                           消息提示：出错时候 提示什么信息 message: '请输入姓名'
                           触发规则：trigger: 'blur'

                    */
                    name: [
                        { required: true, message: '请输入部门名称', trigger: 'blur' }
                    ],
                    sn: [
                        { required: true, message: '请输入部门编号', trigger: 'blur' }
                    ]
                },
                addLoading:false,

            }
        },
        methods:{
            //驳回调用的方法
            reject(){
                //检查是否输入 驳回原因
                if (!this.dataForm.reason){
                    this.$message.error('请输入驳回原因');
                   return;
                }
                //正常的驳回操作
                //组装传递到后台的数据
                let param = {
                    shop_id:this.dataForm.id,
                    note:this.dataForm.reason
                }
                this.$http.post("/shop/reject",param).then(res=>{
                    let {success,message}=res.data;
                    if (success){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                    }else {
                        this.$message.error('网络异常');
                    }
                    this.formVisible=false;
                    //重新加载数据
                    this.loadListData();

                })



            },
            check(index,row){//审核 查看数据
                this.dataForm=Object.assign({}, row);
                this.formVisible=true;
            },
            // index:当前数据的行号  row:就是当前的数据
            handleEdit(index,row){
                //数据拷贝   回显
                this.addForm=Object.assign({}, row);
                //打开弹出框
                this.addFormVisible=true;

            },
            //给数据前面的选择按钮添加事件
            selsChange(val){
                //val 就是用户勾选的数据
                this.sels=val;
            },
            //性别显示转换
            formatSex: function (row, column) {
                return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '未知';
            },
            loadListData(){//远程请求列表数据
                //发送请求获取数据  在main.js 添加 axios.default.baseUrl='/api'
                this.$http.post("/searchMasterMsg",this.query).then(res=>{
                    this.listData=res.data.data;
                    this.total=res.data.total;
                })

            },
            //批量删除
            batchRemove(){
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.patch("/shop",ids).then(res=>{
                        this.listLoading = false;
                        console.debug(res)
                        let {success,message}=res.data;
                        if (success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else {
                            this.$message.error('网络异常');
                        }
                        //重新加载数据
                        this.loadListData();
                    })
                }).catch(() => {

                });


            },
            //点击分页触发的方法
            handleCurrentChange(val){
                //val 就是点击翻页的时候 当前页页码
                this.query.currentPage=val;
                this.loadListData();
            },
            //查询的方法
            search(){
                //初始化开始的页码
               this.query.currentPage=1;
                //重新加载数据
               this.loadListData();
            },
            //新增的方法
            handleAdd(){
                //清空表单
                this.addForm={// 用于数据双向绑定对象
                    name:'',
                    sn:'',
                    state:1,
                    manager:null,
                    parent_id:null
                }
                //打开弹出框
                this.addFormVisible=true;
            },
            //删除的方法  index:当前数据的行号    row:当前行数据
            handleDel(index,row){
               //1.删除的确认框
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(()=>{//确认删除的按钮
                    this.listLoading = true;
                    //获取删除数据id
                    let id= row.id;
                    //发送请求 "/dept/3"
                    this.$http.delete("/shop/"+id).then(res=>{
                        this.listLoading = false;
                        console.debug(res)
                        let {success,message}=res.data;
                        if (success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else {
                            this.$message.error('网络异常');
                        }
                        //重新加载数据
                        this.loadListData();
                    })

                }).catch(()=>{//取消删除回调函数
                    this.$message('已取消删除');
                })
            },
            addSubmit(){
               //开启加载效果
                this.addLoading=true;
                console.debug(this.addForm)
                //发送请求
                this.$http.put("/dept",this.addForm).then(res=>{
                    console.debug(res)
                    let {success,message}=res.data;
                    if (success){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                    }else {
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }
                    //关闭加载效果
                    this.addLoading=false;
                    //关闭弹出框
                    this.addFormVisible=false;
                    //重新加载列表
                    this.loadListData();
                })
            }

        },
        mounted(){
            //页面加载完毕以后立即执行此方法
            this.loadListData();
        }

    }

</script>

<style>



</style>